package com.xing.crm.web.controller;

import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProShop;
import com.xing.crm.pojo.User;
import com.xing.crm.service.ProShopService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年08月05日 15:02
 */
@RequestMapping("/shop")
@Controller
public class ProShopController {
	
	@Resource
	private ProShopService proShopService;
	@Value("${spring.mvc.static-path-pattern}")
	private String imgUriPrefix;
	
	/////////////////////////////////////////////////页面//////////////////////////////////////////////////
	@RequestMapping("/listPage")
	public String list()
	{
		return "admin/shop/list";
	}
	
	
	@RequestMapping("/editPage")
	public String edit(Integer id, Model model)
	{
		ProShop shop = null;
		if(id != null) {
			shop = proShopService.get(id);
		}
		model.addAttribute("shop", shop);
		model.addAttribute("uriPrefix", pathSub());
		return "admin/shop/edit";
	}
	
	/////////////////////////////////////////////////数据//////////////////////////////////////////////////
	@ResponseBody
	@RequestMapping("/list")
	public PageResult<ProShop> list(@RequestParam(required = false, defaultValue = "1") int page,
									@RequestParam(required = false, defaultValue = "10") int limit, ProShop shop)
	{
		return proShopService.list(page, limit, shop);
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public Map<String, Object> edit(ProShop shop, HttpSession session)
	{
		User user = (User) session.getAttribute("user");
		if(user != null) {
			shop.setModifier(user.getId());
		}
		return proShopService.edit(shop);
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public Map<String, Object> delete(Integer id)
	{
		return proShopService.delete(id);
	}
	
	@ResponseBody
	@RequestMapping("/get")
	public ProShop get(Integer id)
	{
		return proShopService.get(id);
	}
	
	private String pathSub()
	{
		if(imgUriPrefix != null)
		{
			return imgUriPrefix.replace("/**", "");
		}
		return "";
	}
}
